package com.ruoyi.readyProcurement.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.readyProcurement.domain.ReadyProcurement;
import com.ruoyi.readyProcurement.domain.ReadyProcurementModel;
import com.ruoyi.readyProcurement.mapper.ReadyMapper;
import com.ruoyi.readyProcurement.service.ReadyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class ReadyServiceImpl implements ReadyService {

    @Autowired
    ReadyMapper readyMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Override
    public int addreadyProcurement(ReadyProcurement readyProcurement) {
        int a = 0;
        if (readyProcurement.getReadyProcurementModels() != null) {
            Map<String, Object> resMap = new HashMap<>();
            try {
                MtAudit mtAudit = new MtAudit();

                a = readyMapper.addreadyProcurement(readyProcurement);
                for (ReadyProcurementModel readyProcurementModel : readyProcurement.getReadyProcurementModels()) {
                    readyProcurementModel.setDeptId(SecurityUtils.getDeptId());
                    readyProcurementModel.setReadyId(readyProcurement.getReadyId());
                    readyMapper.addreadyProcurementModel(readyProcurementModel);

                }
                mtAudit.setOrderId(readyProcurement.getReadyId());
                mtAudit.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
                mtAudit.setOrderType("16");
                mtAudit.setOrderNum(readyProcurement.getReadyOrderNum());
                mtAudit.setAuditStatus("0");
                mtAudit.setZjName("");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);
            } catch (Exception e) {
                a = 0;
                resMap.put("orderId", readyProcurement.getReadyId());
                resMap.put("orderType", "16");
                mtAuditMapper.deleteMtAuditById(resMap);
                readyMapper.removeReadyProcurement(readyProcurement.getReadyId());
                readyMapper.removeReadyProcurementModel(readyProcurement.getReadyId());
            }

        } else {
            return 0;
        }
        return a;
    }

    @Override
    public List<ReadyProcurement> selectReadyProcurements(ReadyProcurement readyProcurement) {
        return readyMapper.selectReadyProcurements(readyProcurement);
    }

    @Override
    public ReadyProcurement selectReadyProcurement(Long readyId) {
        return readyMapper.selectReadyProcurement(readyId);
    }

    @Override
    public int updateReadyProcurement(ReadyProcurement readyProcurement) {
        int num = 0;
        if (readyProcurement.getReadyProcurementModels() != null) {
            readyMapper.removeReadyProcurementModel(readyProcurement.getReadyId());
            for (ReadyProcurementModel readyProcurementModel : readyProcurement.getReadyProcurementModels()) {
                readyProcurementModel.setDeptId(SecurityUtils.getDeptId());
                readyProcurementModel.setReadyId(readyProcurement.getReadyId());
                readyMapper.addreadyProcurementModel(readyProcurementModel);

            }
            num = readyMapper.updateReadyProcurement(readyProcurement);
        } else {
            return num;
        }

        return num;
    }

    @Override
    public void remove(Long readyId) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", readyId);
        resMap.put("orderType", "16");
        mtAuditMapper.deleteMtAuditById(resMap);
        readyMapper.removeReadyProcurement(readyId);
        readyMapper.removeReadyProcurementModel(readyId);
    }

    @Override
    public List<ReadyProcurement> auditReadyProcurement(Long deptId) {
        return readyMapper.auditReadyProcurement(deptId);
    }

    @Override
    public List<ReadyProcurement> auditCheckProcurement(Long deptId) {
        return readyMapper.auditCheckProcurement(deptId);
    }
}
